package m.jclecai.com.pay;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.PayChannelBean;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by zed on 2018/3/12.
 */

public  class RequestPayImpl extends BasePayImpl {


    public RequestPayImpl(BaseActivity activity) {
        super(activity);
    }

    @Override
    public void pay(PayChannelBean channelBean, String payMoney) {
        setPaying(false);
        showDialog();
        getPay(channelBean);
    }

    //channel不包含redirect
    private void getPay(final PayChannelBean payChannelBean) {
        Map<String, String> params = new HashMap<>();
        params.put("body", "Account");
        params.put("total_fee", payChannelBean.getAmount() + "");
        params.put("out_trade_no", payChannelBean.getProjectno());
        setOrderNumber(payChannelBean.getProjectno());
        HttpManager.postAsyn("payment/" + payChannelBean.getKey() + "/request.php", new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay("充值失败");
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                JSONObject obj = FastOk.get(response, JSONObject.class);
                if (obj == null) {
                    DisPlay("该支付通道正在维护，请更换其他支付方式");
                    return;
                }
                if (obj.getInteger("code") != 200) {
                    DisPlay(obj.getString("msg"));
                    return;
                }
                String payUrl = obj.getString("payUrl");
                if (!TextUtils.isEmpty(payUrl))
                    weakUpPay(payUrl);
            }
        }, params);
    }

}
